using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HREngine.Bots
{
    //每个策略的 Penality{策略名}文件里面放 三个函数：打牌评分，随从进攻评分；英雄进攻评分 以及他们需要用到的private函数
    //这三个函数用于单动作评估，如果返回值超过500，则被剪枝，不列入候选动作
    public partial class Behavior大蜥蜴经典T7猎 : Behavior
    {
        public override int getPlayCardPenality(CardDB.Card card, Minion target, Playfield p, Handmanager.Handcard nowHandcard)
        {
            switch (card.nameCN)
            {
                case CardDB.cardNameCN.黑铁矮人:
                case CardDB.cardNameCN.叫嚣的中士:
                    if (p.ownMinions.Count > 0 && target != null && !target.own) return 1000;
                    break;
                case CardDB.cardNameCN.杀戮命令:
                case CardDB.cardNameCN.奥术射击:
                case CardDB.cardNameCN.猎人印记:
                    if (target.own) return 1000;
                    break;
                case CardDB.cardNameCN.铁喙猫头鹰:
                    if(p.enemyMinions.Count>0 && target != null && target.own && !target.frozen)
                    {
                        return 1000;
                    }
                    break;
            }
            return getComboPenality(card, target, p, nowHandcard);
        }

        public override int getAttackWithMininonPenality(Minion m, Playfield p, Minion target)
        {
            // 保留，别送
            int pen = 0;
            if (target.isHero)
            {
                return -20;
            }
            // 能击杀
            if (!target.divineshild && target.Hp <= m.Angr)
            {
                if (target.handcard.card.nameCN == CardDB.cardNameCN.铸甲师)
                {
                    pen -= 10;
                }
                if (m.divineshild)
                {
                    pen -= (target.Hp + target.Angr * 2) * 2;
                }
                else if (m.Hp > target.Angr)
                {
                    pen -= (target.Hp) * 3 - target.Angr;
                }
                else if (m.Hp < 3 && target.Angr > 3)
                {
                    pen -= (target.Hp + target.Angr - m.Hp - m.Angr) * 5;
                }
            }
            else
            {
                if (target.handcard.card.nameCN == CardDB.cardNameCN.铸甲师)
                {
                    pen += 10;
                }
            }
            // 攻击印记随从
            if (m.Angr == 1 && target.Hp == 1 && target.Angr > 2) pen -= 10;

            if (!target.isHero && (target.Angr >= m.Hp || target.poisonous) )
            {
                pen += 3;
            }
            return pen;
        }

        //英雄攻击惩罚
        public override int getAttackWithHeroPenality(Minion target, Playfield p) // 奥秘法没有英雄带刀进攻
        {
            int pen = 0;
            if (target.isHero)
            {
                pen -= 15;
            }
            foreach (Handmanager.Handcard hc in p.owncards)
            {
                if (p.ownWeapon.Durability == 1)
                {
                    if (hc.card.nameCN == CardDB.cardNameCN.鹰角弓)
                    {
                        pen -= 5;
                        break;
                    }
                    else
                    {
                        if (p.ownSecretsIDList.Count > 0)
                        {
                            pen += 35 + (p.ownSecretsIDList.Count * 2);
                            break;
                        }
                        else
                        {
                            if (hc.card.SpellSchool == CardDB.SpellSchool.ARCANE)
                            {
                                pen += 25;
                                break;
                            }
                            else
                            {
                                pen += 18;
                                break;
                            }
                        }
                    }
                }
            }
            return pen;
        }

        private int getSecretPenality(Playfield p)  // 牌序和防奥秘的影响
        {
            // 设置奥秘
            bool set_explosive = false;
            foreach (Action a in p.playactions)
            {
                if (a.actionType == actionEnum.playcard && a.card!=null && a.card.card!=null)
                {
                    if (a.card.card.nameCN ==CardDB.cardNameCN.爆炸陷阱) set_explosive = true;
                }
            }
            if (set_explosive && p.enemyMinions.Count >= 2)
            {
                for (int i = 0; i < p.playactions.Count; i++)
                {
                    Action a = p.playactions[i];
                    // 设置爆炸陷阱的情况下不下怪
                    if (a.actionType == actionEnum.playcard && a.card.card.type == CardDB.cardtype.MOB && !a.card.card.Charge && !a.card.card.Stealth)
                    {
                        return -5000; // 不可接受，抛弃本牌面以及子牌面
                    }
                    // 不打对手随从
                    if (a.actionType == actionEnum.playcard && (a.card.card.nameCN ==CardDB.cardNameCN.杀戮命令 || a.card.card.nameCN ==CardDB.cardNameCN.奥术射击) && a.target != null && a.target != p.enemyHero && a.target.Hp <= 2)
                    {
                        return -5000; // 不可接受，抛弃本牌面以及子牌面
                    }
                    if (a.actionType == actionEnum.attackWithHero || a.actionType == actionEnum.attackWithMinion)
                    {
                        if (a.target != null && a.target.Hp < 3) return -15000;
                    }
                }
            }

            if (p.enemySecretCount == 0)
                return 0;
           
            int pen = 0;

            bool canBe_explosive = false;  //防止是猎人的爆炸陷阱
            foreach (SecretItem si in p.enemySecretList)// 遍历敌人的奥秘
            {
                if (si.canBe_explosive) { canBe_explosive = true; break; }
            }
            if (canBe_explosive)
            {
                int first_attack_hero = -1;
                for (int i = 0; i < p.playactions.Count; i++)
                {
                    Action a = p.playactions[i];
                    if ((a.actionType == actionEnum.attackWithMinion || a.actionType == actionEnum.attackWithHero) && a.target.isHero) // 随从攻击敌方英雄
                    {
                        first_attack_hero = i;
                        if (p.ownHero.Hp <= 2)
                            pen -= 500;
                        break;
                    }
                    // Todo: 这里还要考虑法术伤害敌方英雄 待Fix
                }
                if (first_attack_hero >= 0) // 存在随从攻击英雄
                {
                    //如果此前出牌了，扣分，容易被炸
                    for (int i = 0; i < first_attack_hero; i++)
                    {
                        Action a = p.playactions[i];
                        if (a.actionType == actionEnum.playcard)
                        {
                            if (a.card.card.type == CardDB.cardtype.MOB) //出了随从
                                return -15000; // 不可接受，抛弃本牌面以及子牌面
                        }
                    }
                }

            }
            return pen;
        }
    }
}
